<template>
  <div class="container">
    <div class="blank"><h1>易乎网后台管理中心</h1></div>
    <div class="form">
      <div class="title"><strong>管理员登录</strong></div>
      <el-form ref="loginForm" :model="user" :rules="rules">
        <el-form-item prop="phone">
<!--          <el-input v-model="user.phone"  placeholder="请输入手机号码" suffix-icon="el-icon-mobile-phone"></el-input>-->
          <el-input v-model="user.key"  placeholder="请输入VPN号" suffix-icon="el-icon-mobile-phone"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="user.code" type="password"  placeholder="请输入密码" suffix-icon="el-icon-view"></el-input>
        </el-form-item>
        <el-form-item><el-checkbox v-model="checked">记住密码</el-checkbox></el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="loginById('loginForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data(){
    return {
      "user": {"category": 0,"key":"","code":""},
      "checked": true,
      "msg": "这是登录页面，请完善!",
      rules: {
        key: [
          { validator: this.validPhone, trigger: 'blur' }
        ],
        code: [
          { validator: this.validPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    validPhone: (rule, value, callback) => {
      // if (!value || value.trim() === '') callback(new Error('请输入手机号码'))
      if (!value || value.trim() === '') callback(new Error('请输入需要登录账号VPN'))
      else {
        // const reg = /^[1][3,4,5,7,8][0-9]{9}$/
        // if (!reg.test(value)) callback(new Error('请输入正确的手机号码'))
        if (value.length!==7) callback(new Error('VPN账号长度为7'))
        else callback()
      }
    },
    validPassword: (rule, value, callback) => {
      if (!value || value.trim() === '') callback(new Error('请输入密码'))
      else {
        // const regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,18}')
        const regex = /^[0-9a-zA-Z]{8,18}$/
        if (!regex.test(value)) callback(new Error('密码长度8-18位'))
        else callback()
      }
    },
    loginById: function (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // this.$message({message: this.assert, center: false, type: 'error'})
          const login = await this.$store.dispatch("login", this.user);
          if (login.code === 200) {
            localStorage.setItem('accessToken', login.data.accessToken);
            localStorage.setItem('loginUser', JSON.stringify(login.data.user));
            this.$store.dispatch("useImService",{app:this});
            this.$message.success('登录成功');
            this.$router.replace('/');
          } else {
            this.$message.error(login.msg);
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.container{
  background-color: #d2d6de;
  height: 100%;
}
h1{
  text-align: center;
}
.blank{
  padding-top: 100px;
  padding-bottom: 50px;
}
.form{
  padding: 10px 30px 10px 30px;
  width: 300px;
  margin: 0 auto;
  background-color: white;
}
.title{
  padding: 10px;
  text-align: center;
}
</style>